using System;
using LitJson;
using Framework.Save;
using UnityEngine;

namespace GameCore
{
    public enum ItemCompareMode
    {
        ID, Contains, Exact
    }
    
    public class ItemData : GameData
    {
        public ResourceID ItemID;
        public ObjectData data = new();

        public ItemData(ResourceID itemID)
        {
            ItemID = itemID;
        }

        public ItemData(JsonData json) : base(json) { }

        public void SetData(string key, GameData value) => data[key] = value;
        public bool HasData(string key) => data.ContainsKey(key);
        public GameData GetData(string key) => data[key];
        
        
        public bool Match(ItemData other, ItemCompareMode compareMode = ItemCompareMode.ID)
        {
            return ItemID == other.ItemID;
        }

        public override JsonData Serialize()
        {
            var json = JsonUtil.NewObject();
            json["item_id"] = ItemID.ToString();
            json["data"] = data.Serialize();
            return json;
        }

        public override void Deserialize(JsonData json)
        {
            ItemID = (string)json["item_id"];
            data = new ObjectData(json["data"]);
        }
    }
}